//
//  KAMarketAlertView.swift
//  kano
//
//  Created by Frank on 2024/11/11.
//

import UIKit

class KAMarketAlertView: UIView {
    
    var BLOCK_doneBlock : ((UIButton) -> ())?
    
    var backTapEnable: Bool = true
    
    var var_tap: UITapGestureRecognizer?

//    lazy var var_backgroundView: UIVisualEffectView = {
//        let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.light)
//        let var_backgroundView = UIVisualEffectView(frame: bounds)
//        var_backgroundView.effect = blurEffect
//        var_backgroundView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
//        return var_backgroundView
//    }()
    lazy var var_backgroundView: UIView = {
        let var_backgroundView = UIVisualEffectView(frame: bounds)
        var_backgroundView.frame = bounds
        var_backgroundView.backgroundColor = UIColor.ka_hexColor(0x000000, alphaVaule: 0.66)
        return var_backgroundView
    }()
    
    lazy var var_contentView: UIView = {
        let var_contentView = UIView()
        var_contentView.frame = var_backgroundView.bounds
        var_contentView.backgroundColor = .clear
        return var_contentView
    }()
    
    lazy var var_bgImg: UIImageView = {
        let var_bgImg = UIImageView()
        var_bgImg.isUserInteractionEnabled = true
        //var_bgImg.kf.setImage(with: URL(string: var_model?.imageResourceURL ?? ""))
        var_bgImg.image = UIImage(named: "coupon_bg")
        let var_tap = UITapGestureRecognizer(target: self, action: #selector(tapClick(_ :)))
        var_bgImg.addGestureRecognizer(var_tap)
        return var_bgImg
    }()
    
    @objc func tapClick(_ sender: UITapGestureRecognizer) {
        if handler != nil {
            handler!(1, var_model)
        }
    }
    
    var handler:((Int, KAHomeNewsContent?)->())? = nil
    
    
    lazy var var_closeBtn: UIButton = {
        let var_closeBtn = UIButton()
        var_closeBtn.setBackgroundImage(UIImage(named: "homeAlert_close"), for: .normal)
        var_closeBtn.addTarget(self, action: #selector(ka_closeButtonClick), for: .touchUpInside)
        return var_closeBtn
    }()
    
    @objc func ka_closeButtonClick() {
        guard handler != nil else {
            return
        }
        handler!(0, nil)
    }
    
    var var_model: KAHomeNewsContent?
    
    @objc func ka_show() {
        alpha = 1.0
        let var_rootWindow : UIWindow = UIApplication.shared.keyWindow!
        var_rootWindow.addSubview(self)
        var_backgroundView.alpha = 0

        UIView.animate(withDuration: 0.25, delay: 0.0, animations: { [self] in
            var_backgroundView.alpha = 1
        })
    }
    
    @objc func ka_dismiss() {
        endEditing(true)
        UIView.animate(withDuration: 0.25, delay: 0.0, options: .curveLinear, animations: { [self] in

        }, completion: { [self] _ in
            alpha = 0
            removeFromSuperview()
        })
    }

    
    //MARK: - init
    convenience init(model: KAHomeNewsContent?) {
        self.init(frame: CGRect(x: 0, y: 0, width: var_screenWidth, height: var_screenHeight))
        self.var_model = model
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        ka_setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func ka_setUI() {
        frame = UIScreen.main.bounds
        addSubview(var_backgroundView)
        addSubview(var_contentView)
        var_contentView.addSubview(var_bgImg)
        var_contentView.addSubview(var_closeBtn)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        var_bgImg.snp.makeConstraints { make in
            make.width.equalTo(kscale(280))
            make.height.equalTo(kscale(308))
            make.centerX.equalTo(self.var_contentView)
            make.top.equalTo(8)
            make.bottom.equalTo(kscale(-48))
        }
        var_closeBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(kscale(32), kscale(32)))
            make.centerX.equalTo(self.var_contentView)
            make.bottom.equalTo(0)
        }
        
        var_contentView.snp.makeConstraints { make in
            make.width.equalTo(var_bgImg.snp.width)
            make.height.equalTo(kscale(356))
            make.center.equalTo(self)
        }
    }
}
